<template>
  <div class="login-container">
    <img src="../../assets/img/alogin-back.jpg" alt="" class="login-back" />
    <el-form
      :label-position="right"
      label-width="auto"
      :model="formData"
      :size="big"
      :mode="ruleModel"
      :rules="rules"
      ref="ruleForm"
    >
      <el-form-item>
        <div
          style="
            width: 100%;
            text-align: center;
            font-size: 18px;
            font-weight: 600;
          "
        >
          账号注册
        </div>
      </el-form-item>
      <el-form-item prop="username">
        <el-input
          v-model="formData.username"
          :prefix-icon="User"
          placeholder="手机号"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="formData.password"
          :prefix-icon="Lock"
          type="password"
          clearable
          placeholder="密码"
          maxlength="20"
          show-password
        />
      </el-form-item>
      <!-- <el-form-item prop="code">
        <el-row style="width: 100%" :gutter="20" justify="space-between">
          <el-col :span="16">
            <el-input
              v-model="formData.code"
              :prefix-icon="PieChart"
              clearable
              maxlength="4"
              placeholder="验证码"
            />
          </el-col>
          <el-col :span="8">
            <el-image
              :src="captcha.img"
              style="width: 100%"
              @click="getCaptcha"
            />
          </el-col>
        </el-row>
      </el-form-item> -->
      <el-form-item>
        <el-input
          v-model="formData.email"
          :prefix-icon="Message"
          clearable
          placeholder="邮箱"
          maxlength="20"
        />
        <span class="fcolor" style="font-size: 12px">用于密码找回</span>
      </el-form-item>

      <el-form-item>
        <span>
          已有账号？
          <el-button type="text" @click="toLogin">去登陆</el-button>
        </span>
        <el-button style="width: 100%" type="primary" @click="onRegister">
          注册
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import request from "@/assets/js/request";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { User, Lock, Message } from "@element-plus/icons-vue";

const router = useRouter();

// const captcha = ref({
//   uuid: "",
//   img: "",
// });

const ruleForm = ref();
const rules = ref({
  username: [
    { required: true, message: "手机号必填", trigger: "blur" },
    {
      pattern: /^1[3456789]\d{9}$/,
      message: "手机号码格式不正确",
      trigger: "blur",
    },
  ],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  // code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
});

const ruleModel = ref({
  username: "",
  password: "",
  // code: "",
});

const formData = ref({
  username: "",
  password: "",
  email: "",
  code: "",
  uuid: "",
});

const onRegister = () => {
  ruleForm.value.validate((valid) => {
    if (valid) {
      request
        .post("/common/register", formData.value)
        .then((res) => {
          if (res.code == 200) {
            if (res.code == 200) {
              ElMessage.success("注册成功");
              localStorage.setItem(
                "account",
                JSON.stringify({
                  username: formData.value.username,
                })
              );
              localStorage.setItem("token", res.data.token);
              localStorage.setItem("user", JSON.stringify(res.data.user));
              router.push("/");
            }
          }
        })
        .catch((err) => {
          ElMessage.error(err.message);
        });
    }
  });
};

// 获取验证码
// const getCaptcha = () => {
//   request
//     .get("/common/captcha")
//     .then((res) => {
//       if (res.code == 200) {
//         captcha.value.uuid = res.data.uuid;
//         captcha.value.img = res.data.base64Img;
//       } else {
//         ElMessage({
//           message: res.msg,
//           grouping: true,
//           type: "warning",
//         });
//       }
//     })
//     .catch((err) => {
//       ElMessage.error(err.message);
//     });
// };

// const getAccount = () => {
//   request
//     .get("/common/account")
//     .then((res) => {
//       if (res.code == 200) {
//         formData.value.username = res.data;
//       } else {
//         ElMessage({
//           message: res.msg,
//           grouping: true,
//           type: "warning",
//         });
//       }
//     })
//     .catch((err) => {
//       ElMessage.error(err.message);
//     });
// };

const toLogin = () => {
  router.push("/login");
};

onMounted(() => {
  // getCaptcha();
  // getAccount();
});
</script>

<style scoped>
.demo-tabs > .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}

.demo-tabs {
  background: #fff;
}
</style>
